package ext.tianma.bom.service.AllViewBomDisplay;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 全视图BOM展示controller
 * 
 * @author lilin
 * @date 2018-11-21 00:37:01
 */
@Controller
@RequestMapping({ "/AllViewBomDisplayController" })
public class AllViewBomDisplayController {
	/**
	 * 根据接口参数查询数据并且返回固定格式BOM的字符串
	 * 
	 * @param number
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryAllViewBom", method = { RequestMethod.POST, RequestMethod.GET })
	public void queryAllViewBom(@RequestParam(value = "partNumber", required = true) String partNumber,
			@RequestParam(value = "view", required = false) String view, HttpServletResponse response)
			throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");// 返回文本值
		try {
			String result = AllViewBomDisplayService.getAllViewBom(partNumber, view);// 根据物料编号获取图文档
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

	/**
	 * 判断是否存在物料bom
	 * 
	 * @param number
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryChildren", method = { RequestMethod.POST, RequestMethod.GET })
	public void queryChildren(@RequestParam(value = "partNumber", required = true) String partNumber,
			@RequestParam(value = "view", required = true) String view, HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");// 返回文本值
		try {
			String result = AllViewBomDisplayService.getChildrenPart(partNumber, view);// 根据物料编号获取图文档
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

	/**
	 * 根据接口参数查询数据并且返回固定格式BOM的字符串
	 * 
	 * @param number
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryAllDoc", method = { RequestMethod.POST, RequestMethod.GET })
	public void queryAllDoc(@RequestParam(value = "partNumber", required = true) String partNumber,
			HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");// 返回文本值
		try {
//			String result = AllViewBomDisplayService.getAllDoc(partNumber);// 根据物料编号获取图文档
			String result = AllViewBomDisplayService.getAllReleasedDoc(partNumber);// 根据物料编号获取状态为“已发布”的图文档
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

	/**
	 * 根据接口参数查询数据并且返回固定格式文档压缩包路径的字符串
	 * 
	 * @param number
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryAllDocURL", method = { RequestMethod.POST, RequestMethod.GET })
	public void queryAllDocURL(@RequestBody String list,
			HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");// 返回文本值
		try {
			String result = AllViewBomDisplayService.downloadPrimaryFile(list);// 根据物料编号获取图文档
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据接口参数查询数据并且返回固定格式excel路径的字符串
	 * 
	 * @param number
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryAllBOMExcelURL", method = { RequestMethod.POST, RequestMethod.GET })
	public void queryAllBOMExcelURL(@RequestParam(value = "partNumber", required = true) String partNumber,
			HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");// 返回文本值
		try {
			String result = AllViewBomDisplayService.excelAllViewBOM(partNumber);// 根据物料编号获取图文档
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 查看当前用户是否有当前文档的下载权限
	 * 
	 * @param number
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryPermissions", method = { RequestMethod.POST, RequestMethod.GET })
	public void queryPermissions(@RequestParam(value = "docNumber", required = true) String docNumber,
			HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");// 返回文本值
		try {
			String result = AllViewBomDisplayService.queryPermissions(docNumber);//查看当前用户是否有当前文档的下载权限
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}
